﻿using AutoMapper;
using MediatR;
using T_Person.APi.Applation.CommandHandlers.EmployeeHandlers;
using T_Person.BaseRep.Interface;
using T_Person.Domain;

namespace T_Person.APi.Applation.Commands.EmployeeCommand
{
    public class EmployeeAddCommand : IRequestHandler<EmployeerAddHandlers, int>
    {
        private readonly IEmployeeRep employeeRep;
        private readonly IMapper mapper;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="employeeRep"></param>
        public EmployeeAddCommand(IEmployeeRep employeeRep, IMapper mapper)
        {
            this.employeeRep = employeeRep;
            this.mapper = mapper;
        }
        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(EmployeerAddHandlers request, CancellationToken cancellationToken)
        {
            
            request.CreateBy = "小明";
            request.CreateDate = DateTime.Now;
            var list = mapper.Map<Employee>(request);
            var  list2 = await employeeRep.GetAloneByresult(x => x.Ename == list.Ename && !x.IsDel);
            if (list2!=null)
            {
                return await Task.FromResult(-2);
            }
            await employeeRep.Add(list);
            return await Task.FromResult(list.Eid);
        }
    }
}
